home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / OLIGHT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.3 KB  |  242 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*----------------------------------------------------------------------------
  41.  *
  42.  * olight.c : openGL (motif) example showing how to do hardware lighting
  43.  *            including two_sided lighting.
  44.  *
  45.  * Author : Yusuf Attarwala
  46.  *          SGI - Applications
  47.  * Date   : Mar 93
  48.  *
  49.  *    press  left   button for animation
  50.  *           middle button for two sided lighting (default)
  51.  *           right  button for single sided lighting
  52.  *
  53.  *
  54.  *---------------------------------------------------------------------------*/
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58.  
  59. #include <GL/glut.h>
  60.  
  61. /* function declarations */
  62.  
  63. void
  64.   drawScene(void), setMatrix(void), initLightAndMaterial(void),
  65.   animation(void), resize(int w, int h), menu(int choice), keyboard(unsigned char c, int x, int y);
  66.  
  67. /* global variables */
  68.  
  69. float ax, ay, az;       /* angles for animation */
  70. GLUquadricObj *quadObj; /* used in drawscene */
  71. static float lmodel_twoside[] =
  72. {GL_TRUE};
  73. static float lmodel_oneside[] =
  74. {GL_FALSE};
  75.  
  76. int
  77. main(int argc, char **argv)
  78. {
  79.   glutInit(&argc, argv);
  80.  
  81.   quadObj = gluNewQuadric();  /* this will be used in drawScene 
  82.                                */
  83.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  84.   glutCreateWindow("Two-sided lighting");
  85.  
  86.   ax = 10.0;
  87.   ay = -10.0;
  88.   az = 0.0;
  89.  
  90.   initLightAndMaterial();
  91.  
  92.   glutDisplayFunc(drawScene);
  93.   glutReshapeFunc(resize);
  94.   glutCreateMenu(menu);
  95.   glutAddMenuEntry("Motion", 3);
  96.   glutAddMenuEntry("Two-sided lighting", 1);
  97.   glutAddMenuEntry("One-sided lighting", 2);
  98.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  99.   glutKeyboardFunc(keyboard);
  100.   glutMainLoop();
  101.   return 0;             /* ANSI C requires main to return int. */
  102. }
  103.  
  104. void
  105. drawScene(void)
  106. {
  107.   glClearColor(0.0, 0.0, 0.0, 0.0);
  108.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.  
  110.   glPushMatrix();
  111.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  112.   glColor3f(1.0, 1.0, 0.0);
  113.   glRotatef(ax, 1.0, 0.0, 0.0);
  114.   glRotatef(-ay, 0.0, 1.0, 0.0);
  115.  
  116.   gluCylinder(quadObj, 2.0, 5.0, 10.0, 20, 8);  /* draw a cone */
  117.  
  118.   glPopMatrix();
  119.  
  120.   glutSwapBuffers();
  121. }
  122.  
  123. void
  124. setMatrix(void)
  125. {
  126.   glMatrixMode(GL_PROJECTION);
  127.   glLoadIdentity();
  128.   glOrtho(-15.0, 15.0, -15.0, 15.0, -10.0, 10.0);
  129.   glMatrixMode(GL_MODELVIEW);
  130.   glLoadIdentity();
  131. }
  132.  
  133. int count = 0;
  134.  
  135. void
  136. animation(void)
  137. {
  138.   ax += 5.0;
  139.   ay -= 2.0;
  140.   az += 5.0;
  141.   if (ax >= 360)
  142.     ax = 0.0;
  143.   if (ay <= -360)
  144.     ay = 0.0;
  145.   if (az >= 360)
  146.     az = 0.0;
  147.   drawScene();
  148.   count++;
  149.   if (count >= 60)
  150.     glutIdleFunc(NULL);
  151. }
  152.  
  153. /* ARGSUSED1 */
  154. void
  155. keyboard(unsigned char c, int x, int y)
  156. {
  157.   switch (c) {
  158.   case 27:
  159.     exit(0);
  160.     break;
  161.   default:
  162.     break;
  163.   }
  164. }
  165.  
  166. void
  167. menu(int choice)
  168. {
  169.   switch (choice) {
  170.   case 3:
  171.     count = 0;
  172.     glutIdleFunc(animation);
  173.     break;
  174.   case 2:
  175.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
  176.     glutSetWindowTitle("One-sided lighting");
  177.     glutPostRedisplay();
  178.     break;
  179.   case 1:
  180.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  181.     glutSetWindowTitle("Two-sided lighting");
  182.     glutPostRedisplay();
  183.     break;
  184.   }
  185. }
  186.  
  187. void
  188. resize(int w, int h)
  189. {
  190.   glViewport(0, 0, w, h);
  191.   setMatrix();
  192. }
  193.  
  194. void
  195. initLightAndMaterial(void)
  196. {
  197.   static float ambient[] =
  198.   {0.1, 0.1, 0.1, 1.0};
  199.   static float diffuse[] =
  200.   {0.5, 1.0, 1.0, 1.0};
  201.   static float position[] =
  202.   {90.0, 90.0, 150.0, 0.0};
  203.  
  204.   static float front_mat_shininess[] =
  205.   {60.0};
  206.   static float front_mat_specular[] =
  207.   {0.2, 0.2, 0.2, 1.0};
  208.   static float front_mat_diffuse[] =
  209.   {0.5, 0.5, 0.28, 1.0};
  210.   static float back_mat_shininess[] =
  211.   {60.0};
  212.   static float back_mat_specular[] =
  213.   {0.5, 0.5, 0.2, 1.0};
  214.   static float back_mat_diffuse[] =
  215.   {1.0, 0.9, 0.2, 1.0};
  216.  
  217.   static float lmodel_ambient[] =
  218.   {1.0, 1.0, 1.0, 1.0};
  219.  
  220.   setMatrix();
  221.   glEnable(GL_DEPTH_TEST);
  222.   glDepthFunc(GL_LEQUAL);
  223.  
  224.   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  225.   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  226.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  227.  
  228.   glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  229.   glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  230.   glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  231.   glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  232.   glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  233.   glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  234.  
  235.   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  236.   glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  237.  
  238.   glEnable(GL_LIGHTING);
  239.   glEnable(GL_LIGHT0);
  240.   glShadeModel(GL_SMOOTH);
  241. }
  242.